package com.funever.activity;

import java.util.ArrayList;
import java.util.List;
import net.tsz.afinal.annotation.view.ViewInject;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.funever.combworkalerm.R;
import com.funever.entity.WorkType;
import com.funever.fragment.BaseFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TypeListActivity extends BaseFragment {
	@ViewInject(id=R.id.title)
	TextView nv_title;
	@ViewInject(id=R.id.type_list)
	ListView type_list;
	
	List<WorkType> deleteList = new ArrayList<WorkType>();
	
	List<WorkType> typeList = new ArrayList<WorkType>();
	
	boolean show = false;
	
	boolean allCheck = false;
	
	MyAdapter ma;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mainView = inflater.inflate(R.layout.type_list, null);
		return mainView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		getActivity().setTitle("班类型列表");
        typeList = WorkType.findAll();
        
        ma = new MyAdapter();
        type_list.setAdapter(ma);
	}
	
	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return typeList.size();
		}

		@Override
		public Object getItem(int position) {
			return typeList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			RelativeLayout view = (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.type_list_item, null);
			TextView tv = (TextView) view.findViewById(R.id.type_name);
			tv.setText(typeList.get(position).getName());
			CheckBox checkBox = (CheckBox) view.findViewById(R.id.check);
			checkBox.setChecked(allCheck);
			checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if(isChecked){
							MainActivity activity = (MainActivity) getActivity();
							if(!show){
								activity.startActionMode(new AnActionModeOfEpicProportions());
								deleteList.add(typeList.get(position));
								show = true;
							}
					}else{
						if(deleteList.contains(typeList.get(position))){
							deleteList.remove(typeList.get(position));
						}
					}
				}
			});
			return view;
		}
		
	}
	
	 /** 
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
     */  
    public int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  
    
    public final class AnActionModeOfEpicProportions implements ActionMode.Callback {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            //Used to put dark icons on light action bar
        	MenuItem delete = menu.add(1, 2, 2, "删除");
        	delete.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
            menu.add(1,1,1,"全选")
            .setIcon(android.R.drawable.checkbox_off_background)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//            Toast.makeText(getActivity(), "Got click: " + item, Toast.LENGTH_SHORT).show();
//            mode.finish();
        	if(item.getItemId()==1){
        		if(allCheck){
        			allCheck = false;
        			item.setIcon(android.R.drawable.checkbox_off_background);
        		}else{
        			item.setIcon(android.R.drawable.checkbox_on_background);
        			allCheck = true;
        		}
        		ma.notifyDataSetChanged();
        	}else if(item.getItemId() ==2){
        		for(WorkType wt : deleteList){
        			WorkType.deleteByWhere("name='"+wt.getName()+"'");
        		}
        		allCheck = false;
        		typeList = WorkType.findAll();
        		ma.notifyDataSetChanged();
        		mode.finish();
        	}
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
        	show = false;
        }
    }
}
